<?php 
class Admini_FinancialController extends Uuch_ManageController{
    
    public function init() {
        parent::init();
        $this->view->type="financial";
        $this->view->countfin1 = Application_Model_FinancialCheckItem::countFinByStatu(Application_Model_FinancialCheckItem::Status_Auto);
        $this->view->countfin2 = Application_Model_FinancialCheckItem::countFinByStatu(Application_Model_FinancialCheckItem::Status_Pending);
        $this->view->countfin3 = Application_Model_FinancialCheckItem::countFinByStatu(Application_Model_FinancialCheckItem::Status_Trial);
        $this->view->countfin4 = Application_Model_FinancialCheckItem::countFinByStatu(Application_Model_FinancialCheckItem::Status_Reject);
        $this->appendCss('/admins/css/yundan.css');
        $this->appendCss('/admins/css/common.css');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
    }
    
    public function indexAction()
    {
        $pagesize = $this->getSafeParam('pagesize');
        if (!$pagesize) {
            $pagesize = 10;
        }
        $pageindex = $this->getSafeParam('start');
        if (!$pageindex) {
            $pageindex = 0;
        }
        $this->setTitle('财务手工日志');
        $quick = $this->getSafeParam('quick');
        $timescope = $this->getSafeParam('timescope');
        $starttime = $this->getSafeParam('starttime');
        $endtime = $this->getSafeParam('endtime');
        
        $objectType = $this->getSafeParam("objectType");
        $moneyMax = $this->getSafeParam("moneyMax");
        $moneyMin = $this->getSafeParam("moneyMin");
        $moneyType = $this->getSafeParam("moneyType");
        $status = $this->getSafeParam("status");
        $statusid = $this->getSafeParam("statusid");
        $stype = 99;
        if($status){
            $stype = $status;
        }
        if($statusid){
            $status = $statusid;
        }
        $strart = date("m") % 3 == 0 ? date("m") / 3 : ((int) (date("m") / 3) + 1);
        if ($timescope != null) {
            switch ($timescope) {
                case 'lastweek':
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 7 - 7, date("Y")));
                    break;
                case "week":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0,  date("m"), date("d"), date("Y")));
                    break;
                case "month":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
                case "lastmonth":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m") - 1, cal_days_in_month(CAL_GREGORIAN, date("m") - 1, date("Y")), date("Y")));
                    break;
                case "quarter":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3 + 1, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0,  date("m"), date("d"), date("Y")));
                    break;
                case "lastquarter":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3 - 2, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, ($strart - 1) * 3, cal_days_in_month(CAL_GREGORIAN, ($strart - 1) * 3, date("Y")), date("Y")));
                    break;
                case "year":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, 1, 1, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0,  date("m"), date("d"), date("Y")));
                    break;
                case "lastyear":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, 1, 1, date("Y") - 1));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, 12, cal_days_in_month(CAL_GREGORIAN, 12, date("Y") - 1), date("Y") - 1));
                    break;
                case "day7":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 6, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
                case "day30":
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 30, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
                default :
                    $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 90, date("Y")));
                    $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
                    break;
            }
        } else {
            if ($starttime == null && $endtime == "") {
                $starttime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 90, date("Y")));
                $endtime = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
            }
        }
        $financialFilter = new Application_Model_Filter_FinancialSearch();
        $financialFilter->quick = $quick;
        $financialFilter->starttime = $starttime;
        $financialFilter->endtime = $endtime;
        $financialFilter->objectType = $objectType;
        $financialFilter->moneyMax = $moneyMax;
        $financialFilter->moneyMin = $moneyMin;
        $financialFilter->moneyType = $moneyType;
        $financialFilter->statusid = $status;
        $financialItem = Application_Model_FinancialCheckItem::getFilter($financialFilter,$pagesize,$pageindex);
        $this->view->financialAll = $financialItem;
        $this->view->financialList = $financialItem['list'];
        $this->view->quick = $quick;
        $this->view->timescope = $timescope;
        $this->view->starttime = $starttime;
        $this->view->endtime = $endtime;
        $this->view->pagesize = $pagesize;
        
        $this->view->objectType = $objectType;
        if($objectType){
            $this->view->objectItem = Application_Model_DomainobjectType::load($objectType);
        }
        $this->view->moneyMax = $moneyMax;
        $this->view->moneyMin = $moneyMin;
        $this->view->moneyType = $moneyType;
        $this->view->statusid = $statusid;
        $this->view->pagesize = $pagesize;
        $this->view->stype = $stype;
        
        $this->view->ObjectTypeAll = Application_Model_DomainobjectType::getAll();
        $this->view->MoneyTypeAll = Application_Model_FinancialCheckItem::getMoneyTypeAll();
        $this->view->StatusAll = Application_Model_FinancialCheckItem::getStatusAll();
    }
    
    public function pendingAction()
    {
        $id = $this->getSafeParam("id");
        $financialItem = Application_Model_FinancialCheckItem::load($id);
        if(!$financialItem){
            $this->json(0);
            return ;
        }
        if(Uuch_Util::getUserNikeName() != Application_Model_FinancialCheckItem::Operator_Default){
            $this->json(9);
            return ;
        }
        if($financialItem->Status != Application_Model_FinancialCheckItem::Status_Pending){
            $this->json(2);
            return ;
        }
        $financialItem->dateChecked = date("Y-m-d H:i:s");
        $financialItem->Status = Application_Model_FinancialCheckItem::Status_Trial;
        $financialItem->save();
        $this->json(1);        
    }
    
    public function closeAction()
    {
        $id = $this->getSafeParam("id");
        $financialItem = Application_Model_FinancialCheckItem::load($id);
        if(!$financialItem){
            $this->json(0);
            return ;
        }
        if(Uuch_Util::getUserNikeName() != Application_Model_FinancialCheckItem::Operator_Default){
            $this->json(9);
            return ;
        }
        if($financialItem->Status != Application_Model_FinancialCheckItem::Status_Trial){
            $this->json(2);
            return ;
        }
        $financialItem->dateChecked = date("Y-m-d H:i:s");
        $financialItem->Status = Application_Model_FinancialCheckItem::Status_Close;
        $financialItem->save();
        $this->json(1);       
    }
    
    public function rejectAction()
    {
        $id = $this->getSafeParam("id");
        $financialItem = Application_Model_FinancialCheckItem::load($id);
        if(!$financialItem){
            $this->json(0);
            return ;
        }
        if(Uuch_Util::getUserNikeName() != Application_Model_FinancialCheckItem::Operator_Default){
            $this->json(9);
            return ;
        }
        if($financialItem->Status != Application_Model_FinancialCheckItem::Status_Pending){
            $this->json(2);
            return ;
        }
        $financialItem->dateChecked = date("Y-m-d H:i:s");
        $financialItem->Status = Application_Model_FinancialCheckItem::Status_Reject;
        $financialItem->save();
        $this->json(1);  
    }
    
    public function rejectcloseAction()
    {
        $id = $this->getSafeParam("id");
        $financialItem = Application_Model_FinancialCheckItem::load($id);
        if(!$financialItem){
            $this->json(0);
            return ;
        }
        if(Uuch_Util::getUserNikeName() != Application_Model_FinancialCheckItem::Operator_Default){
            $this->json(9);
            return ;
        }
        if($financialItem->Status != Application_Model_FinancialCheckItem::Status_Reject){
            $this->json(2);
            return ;
        }
        $financialItem->dateChecked = date("Y-m-d H:i:s");
        $financialItem->Status = Application_Model_FinancialCheckItem::status_RejectClose;
        $financialItem->save();
        $this->json(1); 
    }
}